package web.servlet.datasource.book;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @Author Administrator
 * @Date 2020/5/26 14:01
 **/
public class Test {
    private static Scanner sc = new Scanner(System.in);
    private static BookDao dao = new BookDaoDB();

    public static void main(String[] args) {
        System.out.println("--------欢迎光临---------");
        while (true) {
            System.out.println("1添加图书");
            System.out.println("2查询图书");
            System.out.println("3更新图书");
            System.out.println("4删除图书");
            System.out.println("5退出");
            System.out.println("请输入你的选择：");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    insertUI();
                    break;
                case 2:
                    selectUI();
                    break;
                case 3:
                    updateUI();
                    break;
                case 4:
                    deleteUI();
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误！");
                    break;

            }
        }

    }

    private static void insertUI() {
        System.out.println("----------添加图书界面---------");
        System.out.println("请输入图书名称：");
        sc.nextLine();
        String name = sc.nextLine();
        if (name.contains(" ")) {
            System.out.println("图书名称不能为空");
        }
        System.out.println("请输入图书出版社：");
        String press = sc.nextLine();
        if (press.contains(" ")) {
            System.out.println("图书出版社不能为空");
        }
        System.out.println("请输入图书作者:");
        String author = sc.nextLine();
        if (author.contains(" ")) {
            System.out.println("图书作者不能为空");
        }
        System.out.println("请输入图书价格：");
        double price = sc.nextDouble();
        if (price <= 0) {
            System.out.println("图书价格不能为零或负数");
        }
        if (!name.contains(" ") && !press.contains(" ") && !author.contains(" ") && price > 0) {
            Book book = new Book(name, press, author, price);
            int i = dao.insert(book);
            if (i > 0) {
                System.out.println("添加成功！");
            } else if (i == 0) {
                System.out.println("保存失败！");
            } else {
                System.out.println("图书名称重复");
            }
        }

    }

    private static void selectUI() {
        System.out.println("----------查询图书界面------------");
        List<Book> list = dao.selectAll();
        if (list != null) {
            Iterator<Book> iterator = list.iterator();
            while (iterator.hasNext()) {
                Book book = iterator.next();
                System.out.println(book);
            }
        }
    }

    private static void updateUI() {
        System.out.println("---------更新图书界面--------------");
        System.out.println("请输入要修改图书名称：");
        sc.nextLine();
        String name = sc.nextLine();
        Book book1 = dao.selectByName(name);
        if (book1 == null) {
            System.out.println("图书不存在");
        } else {
            System.out.println(book1);
            System.out.println("请输入修改后的图书名称：");
            String n = sc.nextLine();
            System.out.println("请输入图书出版社：");
            String press = sc.nextLine();
            System.out.println("请输入图书作者:");
            String author = sc.nextLine();
            System.out.println("请输入图书价格：");
            double price = sc.nextDouble();
            Book newBook = new Book(book1.getId(), n, press, author, price);
            int i = dao.update(book1.getId(), newBook);
            if (i > 0) {
                System.out.println("更新成功！");
            } else {
                System.out.println("更新失败！");
            }


        }
    }

    private static void deleteUI() {
        System.out.println("---------删除图书界面-------------1");
        System.out.println("请输入要删除的图书名称：");
        sc.nextLine();
        String name = sc.nextLine();
        Book book = dao.selectByName(name);
        if (book == null) {
            System.out.println("要删除的图书不存在！");
        } else {
            int i1 = dao.delete(book.getId());
            if (i1 > 0) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败");
            }
        }

    }
}
